<template>
  <view class="container" :style="appThemeStyle">
    <z-paging ref="paging" v-model="list" class="notice-list" @query="getlist">
      <!-- tab栏 -->
      <template #top>
        <view style="box-shadow: 0px 0px 28rpx 1rpx rgba(78, 101, 153, 0.2)">
          <div style="height: 20rpx; width: 100vw"></div>
          <view style="padding: 0 0  20rpx 0">
            <u-search
              shape="round"
              v-model="keyword"
              @custom="search"
              placeholder="请输入手机号"
            ></u-search>
          </view>
        </view>
      </template>
      <!-- 订单列表 -->
      <view class="order-list">
        <view class="order-item" v-for="(item, index) in list" :key="index">
          <div class="f ac xb" style="margin-top: -20rpx">
            <div>客户：{{ item.orderConsignee.consigneeName || "收件人" }}</div>
            <div>
              联系方式：{{ item.orderConsignee.consigneePhone || "收件人" }}
            </div>
          </div>
          <view class="item-top mt10r">
            <view class="item-top-left">
              <text class="order-time">{{ item.createTime }}</text>
            </view>
            <view class="item-top-right">
              <text class="state-text">{{ item.state_text }}</text>
            </view>
          </view>
          <!-- 商品列表 -->
          <view class="goods-list" @click="handleTargetDetail(item)">
            <view
              class="goods-item"
              v-for="(goods, idx) in item.orderDetails"
              :key="idx"
            >
              <!-- 商品图片 -->
              <view class="goods-image">
                <image
                  class="image"
                  :src="goods.url"
                  mode="scaleToFill"
                ></image>
              </view>
              <!-- 商品信息 -->
              <view class="goods-content">
                <view class="goods-title">
                  <text class="twoline-hide">{{ goods.title }}</text>
                </view>
                <view class="goods-props clearfix">
                  <!-- <view class="goods-props-item" v-for="(props, idx) in goods.goods_props" :key="idx">
                    <text>{{ props.value.name }}</text>
                  </view> -->
                  <div class="goods-props-item">分类：{{ goods.name }}</div>
                </view>
              </view>
              <!-- 交易信息 -->
              <view class="goods-trade">
                <view class="goods-price">
                  <text class="unit"></text>
                  <text class="value"></text>
                </view>
                <view class="goods-num">
                  <text>×{{ goods.num }}</text>
                </view>
              </view>
            </view>
          </view>
          <!-- 订单合计 -->
          <view class="order-total">
            <text>共{{ item.totalNum }}件商品</text>
            <!-- <text class="unit">￥</text>
            <text class="money">
              {{ item.payPrice + Number(item.mailPay) }}
            </text> -->
          </view>
          <!-- 订单操作 -->
          <view
            v-if="item.order_status != OrderStatusEnum.CANCELLED.value"
            class="order-handle"
          >
            <view class="btn-group clearfix">
              <!-- 未支付取消订单 -->
              <block v-if="item.pay_status == PayStatusEnum.PENDING.value">
                <view class="btn-item" @click="onCancel(item.order_id)">
                  取消
                </view>
              </block>
              <!-- 已支付进行中的订单 -->
              <block
                v-if="item.order_status != OrderStatusEnum.APPLY_CANCEL.value"
              >
                <block
                  v-if="
                    item.pay_status == PayStatusEnum.SUCCESS.value &&
                    item.delivery_status ==
                      DeliveryStatusEnum.NOT_DELIVERED.value
                  "
                >
                  <view class="btn-item" @click="onCancel(item.order_id)">
                    申请取消
                  </view>
                </block>
              </block>
              <!-- 已申请取消 -->
              <view v-else class="f-28 col-8">取消申请中</view>
              <!-- 未支付的订单 -->
              <block v-if="item.pay_status == PayStatusEnum.PENDING.value">
                <view class="btn-item active" @click="onPay(item.order_id)">
                  去支付
                </view>
              </block>
              <!-- 确认收货 -->
              <block
                v-if="
                  item.delivery_status == DeliveryStatusEnum.DELIVERED.value &&
                  item.receipt_status == ReceiptStatusEnum.NOT_RECEIVED.value
                "
              >
                <view class="btn-item active" @click="onReceipt(item.order_id)">
                  确认收货
                </view>
              </block>
              <!-- 订单评价 -->
              <block
                v-if="
                  item.order_status == OrderStatusEnum.COMPLETED.value &&
                  item.is_comment == 0
                "
              >
                <view
                  class="btn-item"
                  @click="handleTargetComment(item.order_id)"
                >
                  评价
                </view>
              </block>
            </view>
          </view>
        </view>
      </view>
    </z-paging>
  </view>
</template>

<script>
import {
  DeliveryStatusEnum,
  DeliveryTypeEnum,
  OrderStatusEnum,
  PayStatusEnum,
  ReceiptStatusEnum,
} from "@/common/enum/order";
import { getEmptyPaginateObj, getMoreListData } from "@/core/app";
import * as OrderApi from "@/api/order";
import { adminSearch } from "../../api/shoppingCart";
import ZPaging from "../../uni_modules/z-paging/components/z-paging/z-paging.vue";

// 每页记录数量
const pageSize = 15;

// tab栏数据
const tabs = [
  {
    name: `已下单`,
    value: 0,
  },
  {
    name: `待发货`,
    value: 1,
  },

  {
    name: `待取消`,
    value: 5,
  },
  {
    name: `已取消`,
    value: 2,
  },
  {
    name: `已发货`,
    value: 3,
  },
  {
    name: `已收货`,
    value: "4",
  },
];

export default {
  components: { ZPaging },
  data() {
    return {
      keyword: "",
      // 枚举类
      DeliveryStatusEnum,
      DeliveryTypeEnum,
      OrderStatusEnum,
      PayStatusEnum,
      ReceiptStatusEnum,

      // 当前页面参数
      options: { dataType: "all" },
      // tab栏数据
      tabs,
      // 当前标签索引
      curTab: 0,
      // 订单列表数据
      // list: getEmptyPaginateObj(),
      list: [],
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 初始化当前选中的标签
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.$refs.paging.reload();
  },
  /**
   * 生命周期函数--监听页面的卸载
   */
  onUnload() {
    // 卸载全局事件订阅
    uni.$off("syncRefresh");
  },

  methods: {
    async getlist(pageNo, pageNum = 10, key) {
      let data = {
        key: this.keyword,
        pageSize: pageNum,
        pageNum: pageNo,
  
      };

      const res = await adminSearch(data);
      if (res.rows.length == 0) {
        this.$refs.paging.complete();
        return;
      }
      this.$refs.paging.complete(res.rows);
    },

    /**
     * 上拉加载的回调 (页面初始化时也会执行一次)
     * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
     * @param {Object} page
     */
    upCallback(page) {
      const app = this;
      // 设置列表数据
      app
        .adminSearch(page.num)
        .then((list) => {
          const curPageLen = list.data.length;
          const totalSize = list.data.total;
          app.mescroll.endBySize(curPageLen, totalSize);
        })
        .catch(() => app.mescroll.endErr());
    },

    // 获取订单列表
    adminSearch(pageNo = 1) {
      const app = this;
      return new Promise((resolve, reject) => {
        OrderApi.list(
          { dataType: app.getTabValue(), page: pageNo },
          { load: false }
        ).then((result) => {
          // 合并新数据
          const newList = app.initList(result.data.list);
          app.list.data = getMoreListData(newList, app.list, pageNo);
          resolve(newList);
        });
      });
    },

    // 初始化订单列表数据
    initList(newList) {
      newList.data.forEach((item) => {
        item.total_num = 0;
        item.goods.forEach((goods) => {
          item.total_num += goods.total_num;
        });
      });
      return newList;
    },

    // 获取当前标签项的值
    getTabValue() {
      return this.tabs[this.curTab].value;
    },
    search() {
      this.getlist(1, 10);
    },

    // 切换标签项
    onChangeTab(index) {
      const app = this;
      // 设置当前选中的标签
      app.curTab = index.value;
      // 刷新订单列表
      // app.onRefreshList();
      this.$refs.paging.reload();
    },

    // 刷新订单列表
    onRefreshList() {
      this.list = getEmptyPaginateObj();
      setTimeout(() => {
        this.mescroll.resetUpScroll();
      }, 120);
    },

    // 取消订单
    onCancel(orderId) {
      const app = this;
      uni.showModal({
        title: "友情提示",
        content: "确认要取消该订单吗？",
        success(o) {
          if (o.confirm) {
            OrderApi.cancel(orderId).then((result) => {
              // 显示成功信息
              app.$toast(result.message);
              // 刷新订单列表
              app.onRefreshList();
            });
          }
        },
      });
    },

    // 确认收货
    onReceipt(orderId) {
      const app = this;
      uni.showModal({
        title: "友情提示",
        content: "确认收到商品了吗？",
        success(o) {
          if (o.confirm) {
            OrderApi.receipt(orderId).then((result) => {
              // 显示成功信息
              app.$success(result.message);
              // 刷新订单列表
              app.onRefreshList();
            });
          }
        },
      });
    },

    // 点击去支付
    onPay(orderId) {
      this.$navTo("pages/checkout/cashier/index", { orderId });
    },

    // 跳转到订单详情页
    handleTargetDetail(val) {
      this.$navTo(
        `pages/order/detail1?orderId=${val.id}&orderCode=${val.orderCode}&saleType=${this.curTab}`
      );
    },

    // 跳转到订单评价页
    handleTargetComment(orderId) {
      // this.$navTo("pages/order/comment/index", {orderId});
    },
  },
};
</script>

<style lang="scss" scoped>
// 项目内容
.order-item {
  margin: 20rpx auto 20rpx auto;
  padding: 30rpx 30rpx;
  width: 94%;
  box-shadow: 0px 0px 28rpx 1rpx rgba(78, 101, 153, 0.14);
  border-radius: 16rpx;
  background: #fff;
  box-sizing: border-box;
}

// 项目顶部
.item-top {
  display: flex;
  justify-content: space-between;
  font-size: 26rpx;
  margin-bottom: 40rpx;

  .order-time {
    color: #777;
  }

  .state-text {
    color: $main-bg;
  }
}

// 商品列表
.goods-list {
  // 商品项
  .goods-item {
    display: flex;
    margin-bottom: 40rpx;

    // 商品图片
    .goods-image {
      width: 180rpx;
      height: 180rpx;

      .image {
        display: block;
        width: 100%;
        height: 100%;
        border-radius: 8rpx;
      }
    }

    // 商品内容
    .goods-content {
      flex: 1;
      padding-left: 16rpx;
      padding-top: 16rpx;

      .goods-title {
        font-size: 26rpx;
        max-height: 76rpx;
      }

      .goods-props {
        margin-top: 14rpx;
        color: #ababab;
        font-size: 24rpx;
        overflow: hidden;

        .goods-props-item {
          padding: 4rpx 16rpx;
          border-radius: 12rpx;
          background-color: #fcfcfc;
        }
      }
    }

    // 交易信息
    .goods-trade {
      padding-top: 16rpx;
      width: 150rpx;
      text-align: right;
      color: $uni-text-color-grey;
      font-size: 26rpx;

      .goods-price {
        vertical-align: bottom;
        margin-bottom: 16rpx;

        .unit {
          margin-right: -2rpx;
          font-size: 24rpx;
        }
      }
    }
  }
}

// 订单合计
.order-total {
  font-size: 26rpx;
  vertical-align: bottom;
  text-align: right;
  height: 40rpx;
  margin-bottom: 30rpx;

  .unit {
    margin-left: 8rpx;
    margin-right: -2rpx;
    font-size: 26rpx;
  }

  .money {
    font-size: 28rpx;
  }
}

// 订单操作
.order-handle {
  .btn-group {
    .btn-item {
      border-radius: 10rpx;
      padding: 8rpx 20rpx;
      margin-left: 15rpx;
      font-size: 26rpx;
      float: right;
      color: #383838;
      border: 1rpx solid #a8a8a8;

      &:last-child {
        margin-left: 0;
      }

      &.active {
        color: $main-bg;
        border: 1rpx solid $main-bg;
      }
    }
  }
}

//选中蓝色滑块对应
::v-deep .u-tabs__wrapper__nav[data-v-48634e29] {
  display: flex;
  flex-direction: row;
  position: relative;
  justify-content: space-between;
}
::v-deep .u-tabs__wrapper__nav__line {
  flex: 1 1 0% !important;
}
::v-deep .u-tabs__wrapper__nav__item {
  flex: 1 1 0% !important;
}
</style>
